package com.cnap.multiclouldmgmt.controller;

import com.cnap.multiclouldmgmt.MulticlouldmgmtApplicationTests;
import com.cnap.multiclouldmgmt.model.RepByPage;
import com.cnap.multiclouldmgmt.model.enums.K8sStatusEnum;
import com.cnap.multiclouldmgmt.model.exception.JsonException;
import com.cnap.multiclouldmgmt.model.k8sModels.K8sClusterBasicModel;
import com.cnap.multiclouldmgmt.model.k8sModels.K8sClusterModel;
import com.cnap.multiclouldmgmt.model.k8sModels.NodeModel;
import com.cnap.multiclouldmgmt.model.k8sModels.SvcModel;
import com.cnap.multiclouldmgmt.model.k8sMonitorModels.AlgReqModel;
import com.cnap.multiclouldmgmt.model.vm.CheckDelVmModel;
import com.cnap.multiclouldmgmt.service.AlgInfoService;
import com.cnap.multiclouldmgmt.service.K8sClusterService;
import com.cnap.multiclouldmgmt.service.MultiCloudMgmtService;
import com.cnap.multiclouldmgmt.service.PrometheusService;
import com.cnap.multiclouldmgmt.util.CnapCloudMgmtConstants;
import org.json.JSONException;
import org.junit.jupiter.api.Test;
import org.junit.platform.engine.support.hierarchical.Node;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;

import java.text.ParseException;
import java.util.ArrayList;

import static org.junit.jupiter.api.Assertions.*;

class CnapClusterInfoControllerTest extends MulticlouldmgmtApplicationTests {

    @MockBean
    private MultiCloudMgmtService multiCloudMgmtService;

    @MockBean
    private K8sClusterService k8sClusterService;

    @MockBean
    private AlgInfoService algInfoService;

    @MockBean
    private PrometheusService prometheusService;

    @Autowired
    private CnapClusterInfoController cnapClusterInfoController;

    @Test
    void getAllCloudVendors() {
        cnapClusterInfoController.getAllCloudVendors("");
        cnapClusterInfoController.getAllCloudVendors(CnapCloudMgmtConstants.TEST_ADMIN);
        Mockito.when(multiCloudMgmtService.getCloudVendorsByCreateUser(
                CnapCloudMgmtConstants.TEST_ADMIN)).thenThrow(new JsonException(200,"abc"));
        cnapClusterInfoController.getAllCloudVendors(CnapCloudMgmtConstants.TEST_ADMIN);
    }

    @Test
    void getClusterMonitorInfos() throws Exception {
        cnapClusterInfoController.getClusterMonitorInfos("","","");

        Mockito.when(multiCloudMgmtService.getK8sClusterNum("aliyun",
                "clustername",CnapCloudMgmtConstants.TEST_ADMIN))
                .thenReturn(1);
        RepByPage<K8sClusterModel> k8sClusterInfos = new RepByPage<>();
        k8sClusterInfos.setResult(new ArrayList<>());
        K8sClusterModel k8sClusterModel = new K8sClusterModel();
        k8sClusterInfos.getResult().add(k8sClusterModel);
        K8sClusterModel k8sClusterModelRunning = new K8sClusterModel();
        k8sClusterInfos.getResult().add(k8sClusterModelRunning);

        k8sClusterModelRunning.setStatus(K8sStatusEnum.Running.getChineseDescribe());
        k8sClusterModelRunning.setK8sClusterName("runningClusterName");

        RepByPage<NodeModel> nodeNodels = new RepByPage<>();
        nodeNodels.setResult(new ArrayList<>());
        NodeModel nodeModel = new NodeModel();
        nodeNodels.getResult().add(nodeModel);

        nodeModel.setNodeCpuQuotaByN(1l);
        nodeModel.setNodeCpuUseByN(1l);
        nodeModel.setNodeMemQuotaByKi(1l);
        nodeModel.setNodeMemUseNumByKi(1l);
        nodeModel.setStatus(K8sStatusEnum.Ready.getDescribe());
        nodeModel.setNodeContainerUseNum(1);
        nodeModel.setNodeContainerNormalNum(1);
        nodeModel.setNodeIp("1.1.1.1");
        nodeModel.setNodeName("nodeName");
        

        Mockito.when(multiCloudMgmtService.getK8sClusterInfos
                ("aliyun",
                        "clustername", 1,1,CnapCloudMgmtConstants.TEST_ADMIN))
                .thenReturn(k8sClusterInfos);
        Mockito.when(k8sClusterService.getK8sClusterNodeNum("aliyun",
                "runningClusterName")).thenReturn(1);
        Mockito.when(k8sClusterService.getK8sClusterNodeInfosByConditions("aliyun",
                "runningClusterName",null,1,1)).thenReturn(nodeNodels);

        cnapClusterInfoController.getClusterMonitorInfos("aliyun",
                "clustername",CnapCloudMgmtConstants.TEST_ADMIN);


    }

    @Test
    void getSvcMonitorInfo() throws Exception {

        cnapClusterInfoController.getSvcMonitorInfo("","","");

        Mockito.when(multiCloudMgmtService.getK8sClusterNum("aliyun",
                "clustername",CnapCloudMgmtConstants.TEST_ADMIN))
                .thenReturn(1);

        RepByPage<K8sClusterModel> k8sClusterInfos = new RepByPage<>();
        k8sClusterInfos.setResult(new ArrayList<>());
        K8sClusterModel k8sClusterModel = new K8sClusterModel();
        k8sClusterInfos.getResult().add(k8sClusterModel);
        K8sClusterModel k8sClusterModelRunning = new K8sClusterModel();
        k8sClusterInfos.getResult().add(k8sClusterModelRunning);

        k8sClusterModelRunning.setStatus(K8sStatusEnum.Running.getChineseDescribe());
        k8sClusterModelRunning.setK8sClusterName(CnapCloudMgmtConstants.TEST_ADMIN);

        Mockito.when(multiCloudMgmtService.getK8sClusterInfos
                ("aliyun",
                        "clustername", 1,1,CnapCloudMgmtConstants.TEST_ADMIN))
                .thenReturn(k8sClusterInfos);
        Mockito.when(k8sClusterService.getAllSvcNum(Mockito.anyString(),Mockito.anyString())).thenReturn(1);

        RepByPage<SvcModel> svcModelRepByPage = new RepByPage<>();
        svcModelRepByPage.setResult(new ArrayList<>());
        SvcModel svcModel = new SvcModel();
        svcModelRepByPage.getResult().add(svcModel);
        Mockito.when(k8sClusterService.getK8sClusterSvcs("aliyun",
                "runningClusterName",null,1,1)).thenReturn(svcModelRepByPage);
        cnapClusterInfoController.getSvcMonitorInfo("aliyun",
                "clustername",CnapCloudMgmtConstants.TEST_ADMIN);
    }

    @Test
    void getLoadBalaceDeployAlgInfo() throws Exception {

        AlgReqModel algReqModel = new AlgReqModel();
        algReqModel.setK8sClusterNames(new ArrayList<>());
        cnapClusterInfoController.getLoadBalaceDeployAlgInfo(algReqModel);

        algReqModel.getK8sClusterNames().add("clusterName");
        cnapClusterInfoController.getLoadBalaceDeployAlgInfo(algReqModel);
        Mockito.when(algInfoService.getLoadBalaceDeployAlgInfo(algReqModel)).thenThrow(
                new JsonException(200,"error"));
        cnapClusterInfoController.getLoadBalaceDeployAlgInfo(algReqModel);


    }

    @Test
    void getLoadBalanceInvokeAlgInfo() throws Exception {
        AlgReqModel algReqModel = new AlgReqModel();
        algReqModel.setK8sClusterNames(new ArrayList<>());
        cnapClusterInfoController.getLoadBalaceDeployAlgInfo(algReqModel);

        algReqModel.getK8sClusterNames().add("clusterName");
        cnapClusterInfoController.getLoadBalanceInvokeAlgInfo(algReqModel);
        Mockito.when(algInfoService.getLoadBalanceInvokeAlgInfo(algReqModel)).thenThrow(
                new JsonException(200,"error"));
        cnapClusterInfoController.getLoadBalanceInvokeAlgInfo(algReqModel);
    }

    @Test
    void getK8sClusterEvents() {
        cnapClusterInfoController.getK8sClusterEvents("1","2","3","4",
                100,1,null,null);
        Mockito.when(k8sClusterService.getK8sClusterEvents("1","2","3","4",
                100,1)).thenThrow(new JsonException(200,""));
        cnapClusterInfoController.getK8sClusterEvents("1","2","3","4",
                100,1,null,null);

    }

    @Test
    void getPrometheusRangeData() throws Exception {
        cnapClusterInfoController.getPrometheusRangeData("a","b","c","d");
        Mockito.when(prometheusService.getPrometheusRangeData("a","b","c","d")).thenThrow(new JsonException(200,""));
        cnapClusterInfoController.getPrometheusRangeData("a","b","c","d");


    }

    @Test
    void getNodeMonitorInfo() throws Exception {

        K8sClusterBasicModel k8sClusterBasicModel = new K8sClusterBasicModel();
        k8sClusterBasicModel.setCreateUser(CnapCloudMgmtConstants.TEST_ADMIN);
        k8sClusterBasicModel.setK8sClusterName("clustername");
        Mockito.when(multiCloudMgmtService.getK8sClusterInfoByName("clustername")).thenReturn(k8sClusterBasicModel);

        cnapClusterInfoController.getNodeMonitorInfo("aliyun","clustername","","hello",
                100,1,"ai");

        cnapClusterInfoController.getNodeMonitorInfo("aliyun","clustername","",CnapCloudMgmtConstants.TEST_ADMIN,
                100,1,"ai");

        Mockito.when(k8sClusterService.getK8sClusterNodeInfosByConditions("aliyun","clustername",""
                , "",100,1, CnapCloudMgmtConstants.NODE_APP_TYPE_LABEL + "ai","ai"))
                .thenThrow(new JsonException(200,"222"));
        cnapClusterInfoController.getNodeMonitorInfo("aliyun","clustername","",CnapCloudMgmtConstants.TEST_ADMIN,
                100,1,"ai");

        
    }

    @Test
    void getPodMonitorInfo() throws Exception {

        K8sClusterBasicModel k8sClusterBasicModel = new K8sClusterBasicModel();
        k8sClusterBasicModel.setCreateUser(CnapCloudMgmtConstants.TEST_ADMIN);
        k8sClusterBasicModel.setK8sClusterName("clustername");
        Mockito.when(multiCloudMgmtService.getK8sClusterInfoByName("clustername")).thenReturn(k8sClusterBasicModel);

        cnapClusterInfoController.getPodMonitorInfo("aliyun","clustername","",
                "username","", 100,1);
        cnapClusterInfoController.getPodMonitorInfo("aliyun","clustername","",
                CnapCloudMgmtConstants.TEST_ADMIN,"", 100,1);

        Mockito.when(k8sClusterService.getK8sClusterPods("aliyun","clustername","",null,
                null,null,"",100,1)).thenThrow(new JsonException(200,"aa"));

        cnapClusterInfoController.getPodMonitorInfo("aliyun","clustername","",
                CnapCloudMgmtConstants.TEST_ADMIN,"", 100,1);

    }

    @Test
    void getHpaAlgInfo() throws Exception {


        AlgReqModel algReqModel = new AlgReqModel();
        algReqModel.setK8sClusterNames(new ArrayList<>());
        cnapClusterInfoController.getHpaAlgInfo(algReqModel);

        algReqModel.getK8sClusterNames().add("clusterName");
        cnapClusterInfoController.getHpaAlgInfo(algReqModel);
        Mockito.when(algInfoService.getHpaAlgInfo(algReqModel)).thenThrow(
                new JsonException(200,"error"));
        cnapClusterInfoController.getHpaAlgInfo(algReqModel);
    }

    @Test
    void getK8sClusterInfos() {

        cnapClusterInfoController.getK8sClusterInfos("aliyun","clustername",CnapCloudMgmtConstants.TEST_ADMIN,
                100,1);
        Mockito.when(multiCloudMgmtService.getK8sClusterInfos("aliyun","clustername",
                100,1,CnapCloudMgmtConstants.TEST_ADMIN)).thenThrow( new JsonException(200,"error"));
        cnapClusterInfoController.getK8sClusterInfos("aliyun","clustername",CnapCloudMgmtConstants.TEST_ADMIN,
                100,1);
    }

    @Test
    void getSvcPodFlinks() throws ParseException {

        cnapClusterInfoController.getSvcPodFlinks("clustername","ns","svc","spark");
        cnapClusterInfoController.getSvcPodFlinks("clustername","ns","svc",null);

        Mockito.when(k8sClusterService.getSvcPodFlinks("clustername","ns","svc"))
                .thenThrow(new JsonException(200,"error"));
        cnapClusterInfoController.getSvcPodFlinks("clustername","ns","svc",null);

    }

    @Test
    void getCostOptAlgInfo() throws Exception {

        AlgReqModel algReqModel = new AlgReqModel();
        algReqModel.setK8sClusterNames(new ArrayList<>());
        cnapClusterInfoController.getCostOptAlgInfo(algReqModel);

        algReqModel.getK8sClusterNames().add("clusterName");
        cnapClusterInfoController.getCostOptAlgInfo(algReqModel);
        Mockito.when(algInfoService.getCostAlgInfo(algReqModel)).thenThrow(
                new JsonException(200,"error"));
        cnapClusterInfoController.getCostOptAlgInfo(algReqModel);
    }

    @Test
    void getSvcPodSpark() throws ParseException {

        cnapClusterInfoController.getSvcPodSpark("clustername","ns","svc");

        Mockito.when(k8sClusterService.getSvcPodSpark("clustername","ns","svc"))
                .thenThrow(new JsonException(200,"error"));
        cnapClusterInfoController.getSvcPodSpark("clustername","ns","svc");
    }

    @Test
    void getDynamicExpansionAlgInfo() throws Exception {

        AlgReqModel algReqModel = new AlgReqModel();
        algReqModel.setK8sClusterNames(new ArrayList<>());
        cnapClusterInfoController.getDynamicExpansionAlgInfo(algReqModel);

        algReqModel.getK8sClusterNames().add("clusterName");
        cnapClusterInfoController.getDynamicExpansionAlgInfo(algReqModel);
        Mockito.when(algInfoService.getDynamicExpansionAlgInfo(algReqModel)).thenThrow(
                new JsonException(200,"error"));
        cnapClusterInfoController.getDynamicExpansionAlgInfo(algReqModel);
    }

    @Test
    void checkVmDel() {

        CheckDelVmModel checkDelVmModel = new CheckDelVmModel();
        checkDelVmModel.setCloudType("aliyun");
        cnapClusterInfoController.checkVmDel(checkDelVmModel);
        checkDelVmModel.setVmId("aa");

        cnapClusterInfoController.checkVmDel(checkDelVmModel);

        Mockito.when(multiCloudMgmtService.checkVmDelResult(checkDelVmModel))
                .thenThrow(new JsonException(200,"error"));
        cnapClusterInfoController.checkVmDel(checkDelVmModel);

    }
}